package bma.common.netty;

import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.junit.Test;

import bma.common.langutil.core.ObjectUtil;
import bma.common.langutil.io.HostPort;
import bma.common.netty.handler.ChannelHandlerLog;
import bma.common.netty.proxy.ProxyHandlerSimple;

public class NettyServerTC {

	final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(NettyServerTC.class);

	@Test
	public void base() {
		log.info("start");

		NettyServer s = new NettyServer();
		s.setPort(1234);
		s.setName("test");
		s.addPipeline(new ChannelHandlerLog("test", "DEBUG", "ALL", 32));

		s.start();
		ObjectUtil.waitFor(this, 24 * 60 * 1000);
		s.close();
	}

	@Test
	public void justClose() {
		NettyServer s = new NettyServer();
		s.close();
	}

	@Test
	public void restart() {
		NettyServer s = new NettyServer();
		s.setPort(1234);
		s.setName("test");
		s.addPipeline(new ChannelHandlerLog("test", "DEBUG", "ALL", 32));

		log.info("start1");
		s.start();
		ObjectUtil.waitFor(this, 60 * 1000);
		s.close();

		log.info("start2");
		s.start();
		ObjectUtil.waitFor(this, 60 * 1000);
		s.close();
	}

	@Test
	public void proxy() {
		ClientSocketChannelFactory fac = new NioClientSocketChannelFactory();
		ProxyHandlerSimple p = new ProxyHandlerSimple(fac, new HostPort(
				"172.19.16.195", 9191));

		NettyServer s = new NettyServer();
		s.setPort(9080);
		s.setName("proxy");
		s.addPipeline(p);

		log.info("start proxy");
		s.start();
		ObjectUtil.waitFor(this, 60 * 1000);
		s.close();

	}
}
